package com.luo.bbs.ums.webapi.controller;


import com.luo.bbs.ums.service.IUserService;

import com.luo.common.restful.JsonResult;
import com.luo.pojo.ums.dto.ChangePasswordDTO;
import com.luo.pojo.ums.dto.UserLoginDto;
import com.luo.pojo.ums.dto.UserRegistryDTO;
import com.luo.pojo.ums.dto.UserUpdateDTO;

import com.luo.pojo.ums.vo.TokenVO;
import com.luo.pojo.ums.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户基本（常用）信息表 前端控制器
 * </p>
 *
 * @author bin.cn
 * @since 2023-01-19
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Value("${jwt.tokenHead}")
    private String jwtTokenHead;

    /**
     * 用户注册
     * @param userRegistryDTO
     * @return
     */
    @PostMapping("/registry")
    public JsonResult<Void> registry(@Validated UserRegistryDTO userRegistryDTO){
        userService.registry(userRegistryDTO);
        return JsonResult.ok();
    }

    /**
     * 校验用户名,手机号是否有重复
     *
     * @param value
     * @param type
     */
    @PostMapping("/checkValue")
    public JsonResult<Void> checkValue(String value,String type){
        userService.checkValue(value,type);
        return JsonResult.ok();
    }

    /**
     * 用户登录
     * @param userLoginDto
     * @return jwt
     */
    @PostMapping("/sso/login")
    public JsonResult<TokenVO> login(@Validated UserLoginDto userLoginDto){
        String jwtToken = userService.login(userLoginDto);
        TokenVO tokenVO = new TokenVO();
        tokenVO.setTokenValue(jwtToken);
        tokenVO.setTokenHeader(jwtTokenHead);
        return JsonResult.ok(tokenVO);
    }


    /**
     * 用户登出
     */
    @PostMapping("/sso/logout")
    public JsonResult logout(@RequestHeader(name = "Authorization") String token){
        userService.logout(token);
        return JsonResult.ok();
    }

    /**
     * 修改个人信息
     */
    @PostMapping("/update")
    public JsonResult update(UserUpdateDTO userUpdateDTO){
        UserInfoVO userInfoVO = userService.updateUser(userUpdateDTO);
        return JsonResult.ok(userInfoVO);
    }
    /**
     * 修改密码
     */
    @PostMapping("/renew/password")
    public JsonResult renew(@Validated ChangePasswordDTO changePasswordDTO, @RequestHeader(name = "Authorization") String token){
        userService.renewPassword(changePasswordDTO,token);
        return JsonResult.ok();
    }
}
